package wt.hpad.model.holder;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.implments.SwipeItemMangerImpl;

import java.util.Map;

import wt.hpad.ConstantFields;
import wt.hpad.R;
import wt.hpad.base.BaseHolder;
import wt.hpad.base.BaseRvSwAdapter;
import wt.hpad.dialog.SimpleCheckboxDialog;
import wt.hpad.dialog.EditTextDialog;
import wt.hpad.dialog.SimpleDialogHandler;
import wt.hpad.model.bean.DishDetail;

/**
 * Created by wt on 1.3.
 */

public class DishCartSwHolder extends BaseHolder<DishDetail> {

    private final BaseRvSwAdapter<DishDetail> mAdapter;
    private final Context mContext;
    private Activity mActivity;
    SwipeItemMangerImpl mItemManger;
    private TextView tvDishName;
    private TextView tvDishNum;
    private TextView tvTip;
    private SwipeLayout swipeLayout;
    private ImageView imgSub;
    private ImageView imgAdd;
    private TextView tvDeleteCartItem;
    private View vLine;
    private Button btn_tip_cart;

    public DishCartSwHolder(Context context, ViewGroup parent, BaseRvSwAdapter<DishDetail> adapter, int layoutID) {
        super(context, parent, adapter, layoutID);
        mAdapter = adapter;
        mContext = context;
    }

    public void setActivity(Activity mActivity) {
        this.mActivity = mActivity;
    }

    @Override
    public void setItemView(View itemView) {
        tvDishName = (TextView) itemView.findViewById(R.id.tv_dishName_cart);
        tvDishNum = (TextView) itemView.findViewById(R.id.tv_dishNum_cart);
        tvTip = (TextView) itemView.findViewById(R.id.tv_tip_cart);
        imgSub = (ImageView) itemView.findViewById(R.id.img_sub);
        imgAdd = (ImageView) itemView.findViewById(R.id.img_add);
        tvDeleteCartItem = (TextView) itemView.findViewById(R.id.tv_delete_cart);
        btn_tip_cart = (Button) itemView.findViewById(R.id.btn_tip_cart);
        vLine = itemView.findViewById(R.id.v_line);

        swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe_dishcart);
        swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
        Map<SwipeLayout.DragEdge, View> dragEdgeMap =
                swipeLayout.getDragEdgeMap();
        dragEdgeMap.clear();
        swipeLayout.addDrag(SwipeLayout.DragEdge.Left, itemView.findViewById(R.id.bottom_wrapper_cart));
        swipeLayout.setDragDistance(20);
        swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
            @Override
            public void onDoubleClick(SwipeLayout layout, boolean surface) {
                mItemManger.closeAllItems();
            }
        });

    }

    // 挂接事件
    private void initData(final DishDetail bean) {
        // "减" 数量
        imgSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int dishNum = --(bean.dishNum);

                if (dishNum <= 1) {
                    bean.dishNum = 1;
                    tvDishNum.setText(String.valueOf(1));
                } else {
                    tvDishNum.setText(String.valueOf(dishNum));
                }
            }
        });

        // "加" 数量
        imgAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int dishNum = ++bean.dishNum;
                tvDishNum.setText(String.valueOf(dishNum));
            }
        });

        // "删除" 当前item
        tvDeleteCartItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemManger.removeShownLayouts(swipeLayout);
                int pos = getLayoutPosition();
                ConstantFields.CART_CURRENT.remove(pos);
                mAdapter.removeItem(pos);
                mItemManger.closeAllItems();
            }
        });

        // "备注" 添加
        btn_tip_cart.setOnClickListener(new View.OnClickListener() {
            private EditTextDialog editTextDialog;
            private String customTip = "";

            @Override
            public void onClick(View v) {
                // "口味"选择对话框
                final SimpleCheckboxDialog cbDialog
                        = new SimpleCheckboxDialog(mContext, mActivity,
                        R.layout.dialog_checkbox_tipofcook,
                        R.id.btn_dialog_confirm,
                        R.id.btn_dialog_cancel);
                final int[] rids
                        = new int[]{R.id.cb_1, R.id.cb_2, R.id.cb_3,
                        R.id.cb_4, R.id.cb_5, R.id.cb_6,
                        R.id.cb_7, R.id.cb_8, R.id.cb_9,
                        R.id.cb_10, R.id.cb_11, R.id.cb_12,
                        R.id.tv_custom_tip
                };
                cbDialog.show();

                View rootView = cbDialog.getRootView();
                final CheckBox cb_custom_tip = (CheckBox) rootView.findViewById(R.id.tv_custom_tip);

                final SimpleDialogHandler cbHandler = new SimpleDialogHandler() {
                    @Override
                    public void onConfirm() {
                        String checkedTexts = cbDialog.getCheckedTexts(rids, ",");
                        bean.tipOfCook = checkedTexts;
                        mAdapter.notifyItemChanged(getLayoutPosition());
                        mItemManger.closeItem(getLayoutPosition());
                    }

                    @Override
                    public void onCancel() {
                    }
                };
                cbDialog.setHandler(cbHandler);

                final SimpleDialogHandler edtHandler = new SimpleDialogHandler() {

                    @Override
                    public void onConfirm() {
                        customTip = editTextDialog.getEdtText();
                        if (!TextUtils.isEmpty(customTip)) {
                            cb_custom_tip.setText(customTip);
                        }
                    }

                    @Override
                    public void onCancel() {
                    }
                };
                cb_custom_tip.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // "输入其他口味" 对话框
                        editTextDialog = new EditTextDialog(mActivity);
                        editTextDialog.setHandler(edtHandler);
                        editTextDialog.show();
                    }
                });

                mItemManger.closeItem(getLayoutPosition());
            }
        });
    }

    @Override
    public void bindItemValue(DishDetail bean) {
        isHideSomeItem(bean);
        tvDishName.setText(bean.dishName);
        tvDishNum.setText(String.valueOf(bean.dishNum));
        initData(bean);
    }

    @Override
    public void bindSwItemValue(SwipeItemMangerImpl itemManger, DishDetail bean, final int position) {
        bindItemValue(bean);
        mItemManger = itemManger;
        mItemManger.bindView(this.itemView, position);
    }

    /**
     * 如果没有"口味"则隐藏之
     */
    private void isHideSomeItem(DishDetail bean) {
        if (TextUtils.isEmpty(bean.tipOfCook)) {
            tvTip.setVisibility(View.GONE);
            vLine.setVisibility(View.GONE);
        } else {
            tvTip.setVisibility(View.VISIBLE);
            vLine.setVisibility(View.VISIBLE);
            tvTip.setText(bean.tipOfCook);
        }
    }
}
